package com.mk.admin.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.mk.admin.mapper.MedicalTechnologyReportMapper;
import com.mk.admin.model.ResultDto;
import com.mk.admin.model.dto.MedicalTechnologyReportDto;
import com.mk.admin.model.entity.MedicalTechnologyReportEntity;
import com.mk.admin.model.query.MedicalTechnologyReportQuery;
import com.mk.admin.model.query.MedicalTechnologyReportResultRemoteQuery;
import com.mk.admin.model.vo.MedicalTechnologyReportRemoteVo;
import com.mk.admin.model.vo.MedicalTechnologyReportVo;
import com.mk.admin.service.MedicalTechnologyReportService;
import com.mk.admin.util.HttpTool;
import com.mk.admin.util.JSONCopy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MedicalTechnologyReportServiceImpl implements MedicalTechnologyReportService {
    @Autowired
    private MedicalTechnologyReportMapper medicalTechnologyReportMapper;

    @Override
    public ResultDto check(MedicalTechnologyReportRemoteVo vo) {
        JSONObject jsonObject = HttpTool.postJson("com_0003", JSONObject.toJSONString(vo));
        Object result = jsonObject.get("result");
        if (result != null) {
            List<MedicalTechnologyReportDto> list = JSONArray.parseArray(result.toString(), MedicalTechnologyReportDto.class);
            if (list.size() > 0) {
                for (MedicalTechnologyReportDto item : list) {
                    MedicalTechnologyReportEntity entity = new MedicalTechnologyReportEntity();
                    entity.setReportBillNo(item.getReportBillNo());
//                    先去查询数据库是否有值，如果有，则不管，么有则新增
                    List<MedicalTechnologyReportDto> reportDtoList = medicalTechnologyReportMapper.select(entity);
                    entity = JSONCopy.copy(item, MedicalTechnologyReportEntity.class);
                    entity.setPatId(vo.getQueryValue());
<<<<<<< HEAD
                    entity.setMedicalType("05");
=======
                    entity.setMedicalType("06");
>>>>>>> f2106939b942a7ac2c3470910120a35364fbfaac
                    entity.setMedicalTypeName("检查");
                    if (reportDtoList.size() == 0) {
                        medicalTechnologyReportMapper.insert(entity);
                    } else {
                        entity.setId(reportDtoList.get(0).getId());
                        medicalTechnologyReportMapper.updateById(entity);
                    }
                }
            }
        }
        if (vo.getQueryValue() != null) {
            MedicalTechnologyReportEntity entity = new MedicalTechnologyReportEntity();
            entity.setPatId(vo.getQueryValue());
            entity.setMedicalType(vo.getMedicalType());
            entity.setStartReportTime(vo.getBeginTime());
            entity.setEndReportTime(vo.getEndTime());
            entity.setOrderBy("update_date desc");
            entity.setReportTypeName(vo.getReportTypeName());
            return ResultDto.data(medicalTechnologyReportMapper.select(entity));
        }
        return ResultDto.data("");
    }

    @Override
    public ResultDto select(MedicalTechnologyReportQuery medicalTechnologyReportQuery) {
        MedicalTechnologyReportEntity entity = JSONCopy.copy(medicalTechnologyReportQuery, MedicalTechnologyReportEntity.class);

        return ResultDto.data(medicalTechnologyReportMapper.count(entity), medicalTechnologyReportMapper.select(entity));
    }

    @Override
    public ResultDto checkResult(MedicalTechnologyReportResultRemoteQuery query) {
        JSONObject jsonObject = HttpTool.postJson("com_0005", query);
        Object result = jsonObject.get("result");
        if (result != null) {
            return ResultDto.data(result);
        }
        return ResultDto.data(jsonObject);
    }

    @Override
    public ResultDto testResult(MedicalTechnologyReportResultRemoteQuery query) {
        JSONObject jsonObject = HttpTool.postJson("com_0006", query);
        Object result = jsonObject.get("result");
        if (result != null) {
            return ResultDto.data(result);
        }
        return ResultDto.data(jsonObject);
    }

    @Override
    public ResultDto test(MedicalTechnologyReportRemoteVo vo) {
        JSONObject jsonObject = HttpTool.postJson("com_0004", JSONObject.toJSONString(vo));
        Object result = jsonObject.get("result");
        if (result != null) {
            List<MedicalTechnologyReportDto> list = JSONArray.parseArray(result.toString(), MedicalTechnologyReportDto.class);
            if (list.size() > 0) {
                list.forEach(item -> {
                    MedicalTechnologyReportEntity entity = new MedicalTechnologyReportEntity();
                    entity.setReportBillNo(item.getReportBillNo());
//                    先去查询数据库是否有值，如果有，则不管，么有则新增
                    List<MedicalTechnologyReportDto> reportDtoList = medicalTechnologyReportMapper.select(entity);
                    entity = JSONCopy.copy(item, MedicalTechnologyReportEntity.class);
<<<<<<< HEAD
                    entity.setMedicalType("06");
=======
                    entity.setMedicalType("05");
>>>>>>> f2106939b942a7ac2c3470910120a35364fbfaac
                    entity.setPatId(vo.getQueryValue());
                    entity.setMedicalTypeName("检验");
                    if (reportDtoList.size() == 0) {
                        medicalTechnologyReportMapper.insert(entity);
                    } else {
                        entity.setId(reportDtoList.get(0).getId());
                        medicalTechnologyReportMapper.updateById(entity);
                    }
                });
            }
        }
        if (vo.getQueryValue() != null) {
            MedicalTechnologyReportEntity entity = new MedicalTechnologyReportEntity();
            entity.setPatId(vo.getQueryValue());
            entity.setMedicalType(vo.getMedicalType());
<<<<<<< HEAD
=======
            entity.setStartReportTime(vo.getBeginTime());
            entity.setEndReportTime(vo.getEndTime());
            entity.setOrderBy("update_date desc");
            entity.setReportTypeName(vo.getReportTypeName());
>>>>>>> f2106939b942a7ac2c3470910120a35364fbfaac
            return ResultDto.data(medicalTechnologyReportMapper.select(entity));
        }
        return ResultDto.data("");
    }


    @Override
    public ResultDto add(MedicalTechnologyReportVo medicalTechnologyReportVo) {
        MedicalTechnologyReportEntity entity = JSONCopy.copy(medicalTechnologyReportVo, MedicalTechnologyReportEntity.class);

        return ResultDto.data(medicalTechnologyReportMapper.insert(entity));
    }

    @Override
    public ResultDto update(MedicalTechnologyReportVo medicalTechnologyReportVo) {
        MedicalTechnologyReportEntity entity = JSONCopy.copy(medicalTechnologyReportVo, MedicalTechnologyReportEntity.class);

        return ResultDto.data(medicalTechnologyReportMapper.updateById(entity));
    }

    @Override
    public ResultDto delete(int id) {
        return ResultDto.data(medicalTechnologyReportMapper.deleteById(id));
    }

    @Override
    public ResultDto status(int id) {
        return ResultDto.data(medicalTechnologyReportMapper.status(id));
    }
}
